package com.evon.yardmanagement.export;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.evon.yardmanagement.model.ParkingSlot;
import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.service.SectionManagementService;

public class ParkingSlotExportableData implements ExportableData,
		ImportableData {

	private static final int COLUMN_SLOT_ID = 0;

	private static final int COLUMN_SECTION_ID = 1;

	private static final int COLUMN_SLOT_NUMBER = 2;

	private static final int COLUMN_SLOT_NAME = 3;

	private static final int COLUMN_PRIORITY = 4;

	private static final int COLUMN_OCCUPIED = 5;

	private static final int COLUMN_ACTIVE = 6;

	private static final int COLUMN_UPDATED_USER = 10;

	private static final int COLUMN_CREATED_USER = 9;

	private static final int COLUMN_UPDATED_TIME = 8;

	private static final int COLUMN_CREATED_TIME = 7;

	private static final String PARKING_SLOTS = "PARKING SLOTS";

	private static final String SLOT_ID = "SLOT_ID";

	private static final String SECTION_ID = "SECTION_ID";

	private static final String SLOT_NUMBER = "SLOT_NUMBER";

	private static final String SLOT_NAME = "SLOT_NAME";

	private static final String PRIORITY = "PRIORITY";

	private static final String OCCUPIED = "OCCUPIED";

	private static final String ACTIVE = "ACTIVE";

	private static final String HSC_ID = "HSC_ID";

	private static final String HSU_ID = "HSC_ID";

	private static final String HSU_TS = "HSU_TS";

	private static final String HSC_TS = "HSC_TS";

	private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	private List<ParkingSlot> parkingDataList;

	private File fileToImport;

	public ParkingSlotExportableData(List<ParkingSlot> listParkingSlot) {
		this.parkingDataList = listParkingSlot;

	}

	public int getColumnCount() {
		return 10;
	}

	/**
	 * Return the wrapped data.
	 * 
	 * @return
	 */
	public List<ParkingSlot> getData() {
		return parkingDataList;
	}

	public int getRowCount() {
		if (parkingDataList == null || parkingDataList.isEmpty()) {
			return 0;
		}
		return parkingDataList.size();
	}

	public List<String> getColumnHeader() {
		// TODO Auto-generated method stub
		return Arrays.asList(SLOT_ID, SECTION_ID, SLOT_NUMBER, SLOT_NAME,
				PRIORITY, OCCUPIED, ACTIVE, HSC_TS, HSU_TS, HSC_ID, HSU_ID);
	}

	/**
	 * 
	 */
	public String getValueAt(int row, int column) {
		if (parkingDataList == null || parkingDataList.isEmpty()) {
			throw new IllegalStateException(
					"The ParkingSlotExportableData has no model set, please set the list of model first before calling this method.");
		}
		if (row >= parkingDataList.size() || column > getColumnCount()) {
			throw new IllegalArgumentException(
					"The row and column range is out of bound. Chcek the row and column size being passed to this method.");
		}

		ParkingSlot parkingSlot = parkingDataList.get(row);

		if (parkingSlot == null) {
			throw new IllegalStateException(
					"ParkingSlot inside sectionDataList is null.");
		}

		switch (column) {
		case COLUMN_SLOT_ID:
			return String.valueOf(parkingSlot.getId());
		case COLUMN_SECTION_ID:
			return String.valueOf(parkingSlot.getSection().getId());
		case COLUMN_SLOT_NUMBER:
			return String.valueOf(parkingSlot.getNumber());
		case COLUMN_SLOT_NAME:
			return String.valueOf(parkingSlot.getName());
		case COLUMN_PRIORITY:
			return String.valueOf(parkingSlot.getPriority());
		case COLUMN_OCCUPIED:
			return String.valueOf(parkingSlot.getOccupied());
		case COLUMN_ACTIVE:
			return String.valueOf(parkingSlot.getActive());
		case COLUMN_CREATED_TIME:
			if (parkingSlot.getCreatedTime() == null)
				return formatter.format(new Date());
			return formatter.format(parkingSlot.getCreatedTime());
		case COLUMN_UPDATED_TIME:
			if (parkingSlot.getLastUpdatedTime() == null)
				return formatter.format(new Date());
			return formatter.format(parkingSlot.getLastUpdatedTime());
		case COLUMN_CREATED_USER:
			return parkingSlot.getCreatedUserName();
		case COLUMN_UPDATED_USER:
			return parkingSlot.getLastUpdatedUserName();

		default:
			return "";
		}

	}

	public String getDataName() {
		// TODO Auto-generated method stub
		return PARKING_SLOTS;
	}

	public void setValueAt(int row, int column, Object value) {
		if (parkingDataList == null) {
			parkingDataList = new ArrayList<ParkingSlot>();
		}
		if (column > getColumnCount()) {
			throw new IllegalArgumentException(
					"Column size can not be greater than " + getColumnCount());
		}

		if (parkingDataList.size() - 1 < row) {
			parkingDataList.add(new ParkingSlot());
		}
		ParkingSlot parkingSlot = parkingDataList.get(row);

		switch (column) {
		case COLUMN_SLOT_ID:
			if (value instanceof String)
				parkingSlot.setId(Integer.valueOf((String) value));
			else
				parkingSlot.setId((Integer) value);
			break;
		case COLUMN_SECTION_ID:
			int id = 0;
			if (value instanceof String)
				id = Integer.valueOf((String) value);
			else
				id = (Integer) value;
			List<Section> sections = new SectionManagementService()
					.getSectionBySectionId(id);
			if (sections != null && !sections.isEmpty())
				parkingSlot.setSection(sections.get(0));
			break;
		case COLUMN_SLOT_NUMBER:
			if (value instanceof String)
				parkingSlot.setNumber(Integer.valueOf((String) value));
			else
				parkingSlot.setNumber((Integer) value);
			break;
		case COLUMN_SLOT_NAME:
			parkingSlot.setName((String) value);
			break;
		case COLUMN_PRIORITY:
			if (value instanceof String)
				parkingSlot.setPriority(Integer.valueOf((String) value));
			else
				parkingSlot.setPriority((Integer) value);
			break;
		case COLUMN_OCCUPIED:
			if (value instanceof String)
				parkingSlot.setOccupied(Integer.valueOf((String) value));
			else
				parkingSlot.setOccupied((Integer) value);
			break;
		case COLUMN_ACTIVE:
			if (value instanceof String)
				parkingSlot.setActive(Integer.valueOf((String) value));
			else

				parkingSlot.setActive((Integer) value);
			break;
		case COLUMN_CREATED_TIME:
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d;
			try {
				d = (Date) formatter.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d = new java.util.Date();
			}
			parkingSlot.setCreatedDate(d);
			break;
		case COLUMN_UPDATED_TIME:
			DateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d1;
			try {
				d1 = (Date) formatter1.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d1 = new java.util.Date();
			}
			parkingSlot.setUpdatedDate(d1);
			break;

		case COLUMN_CREATED_USER:
			parkingSlot.setCreatedBy((String) value);
			break;

		case COLUMN_UPDATED_USER:
			parkingSlot.setUpdatedBy((String) value);
			break;

		default:
			throw new IllegalArgumentException("Column size is not correct- "
					+ column);
		}
	}

	public File getFileToImport() {
		// TODO Auto-generated method stub
		return fileToImport;
	}

	public void setFileToImport(File fileToImport) {
		this.fileToImport = fileToImport;

	}

	public ExportCellType getColumnClass(int columnNumber) {
		switch (columnNumber) {
		case COLUMN_SLOT_ID:
			return ExportCellType.NUMBER;
		case COLUMN_SECTION_ID:
			return ExportCellType.NUMBER;
		case COLUMN_SLOT_NUMBER:
			return ExportCellType.NUMBER;
		case COLUMN_SLOT_NAME:
			return ExportCellType.STRING;
		case COLUMN_PRIORITY:
			return ExportCellType.NUMBER;
		case COLUMN_OCCUPIED:
			return ExportCellType.NUMBER;
		case COLUMN_ACTIVE:
			return ExportCellType.NUMBER;
		case COLUMN_CREATED_TIME:
			return ExportCellType.DATE;
		case COLUMN_UPDATED_TIME:
			return ExportCellType.DATE;

		case COLUMN_CREATED_USER:
			return ExportCellType.STRING;

		case COLUMN_UPDATED_USER:
			return ExportCellType.STRING;

		default:
			throw new IllegalArgumentException("Column size is not correct- "
					+ columnNumber);
		}
	}

}
